jQuery(document).ready(
    function(){
        // Save the state of clickable element
        var open = false; // hours clicked already
        var oldFigure = ""; // To remember what was the old figure
        var imgPen = "<img alt=\"edit\" style=\"vertical-align:text-bottom;\" src=\"/MegaSoft/images/skin/pencil.png\"/>";
        var imgLoad = "<img class=\"loading-img\" src=\"/MegaSoft/images/skin/loading4.gif\"/>";
/********************************************************
 * INCOMES CLICKABLE -
 *  - Project Detailed view
 ********************************************************/

        $(".incomeFigures").live("dblclick", function(){
            if(!open){
                open = true;
                oldFigure = $(this).text();
                $(this).html("<input class=\"incomeInput\" id=\""+$(this).attr('id')+"\" type=\"text\" value =\""+$(this).text()+"\"/>");
                $(".incomeInput").focus();
            }
            else{
              $('.incomeInput').blur();
              open = false;
              oldFigure = "";
            }
        });

        $(".incomeInput").livequery(function(){
            $(this).keypress(function(e){
               if(e.which == "13"){
                   $(this).blur();
               }
               else if(e.which == 0){
                   //alert("youhuo");
                   //location.reload(true);
                   $(this).val(oldFigure);
                   $(this).blur();
               }
               else{
                 //  alert(e.which);
               }
            });
            $(this).blur(function(){
                open = false;
                var href = $(".urlUpdateIncome").attr("href");
                var id = $("#projectId").val();
                var type = $(this).attr('id');
                var val = $(this).val();
                val = validNumber(val);
                if(oldFigure != val ){
                     $(this).parent().text(val);
                     $("#"+type).append(imgLoad);
                     //alert( $("#"+type).attr('id'));
                     $.ajax({
                            url: href,
                            data: "ajaxCall=true&id="+id+"&typeOfIncome="+type+"&figure="+val,
                            cache: false,
                            async: true,
                            success: function(html){
                               showAlert = false ;
                              // location.reload(true);
                               $(".body").html(html);
                            }
                     });
                }
                else{
                    $(this).parent().html(oldFigure + imgPen);

                }
                //$(".loading-img").remove();
                oldFigure = "";
            })
        });


/********************************************************
 * WORK HOURS FIGURES CLICKABLE -
 *  - Employee Detailed view
 ********************************************************/



        $(".editableFigures").live("dblclick", function(){ // The the figures that are written on the page
            if(!open){
                open = true;
                 oldFigure = $(this).text();
                $(this).html("<input class=\"figureInput\" id=\""+$(this).attr('id')+"\" type=\"text\" value =\""+$(this).text()+"\"/>");
                $(".figureInput").focus();
            }
            else{
              $('.figureInput').blur();
              open = false;
              oldFigure = "";
            }

        });

        $(".figureInput").livequery(function(){ // The input that will replace the old one
             $(this).keypress(function(e){
               if(e.which == "13"){
                   $(this).blur();
               }
               else if(e.which == 0){
                   //alert("youhuo");
                   //location.reload(true);
                   $(this).val(oldFigure);
                   $(this).blur();
               }
               else{
                 //  alert(e.which);
               }
            });
            $(this).blur(function(){
                open = false;
                var href = $(".urlUpdate").attr("href");
                var id = $("#ajaxEmployeeId").val();    // gets the employeeInstance.Id from bottom of Show
                var type = $(this).attr('id');
                var val = $(this).val();
                val = validNumber(val);
                
                if(oldFigure != val ){
                    $(this).parent().text(val);
                    $("#"+type).append(imgLoad);
                     $.ajax({
                            url: href,
                            data: "page=emp&ajaxCall=true&id="+id+"&type="+type+"&figure="+val,
                            cache: false,
                            async: true,
                            success: function(html){
                                showAlert = false ;
                              //  location.reload(true)
                               $(".body").html(html);
                            }
                     });
                }
                else{
                    $(this).parent().html(oldFigure + imgPen);
                }
                //$(".loading-img").remove();
                oldFigure = "";
            })
        });



/********************************************************
 * VARIABLE COST CLICKABLE -
 *  - Project Detailed view
 ********************************************************/



        $(".variableCostFigures").live("dblclick", function(){
            if(!open){
                open = true;
                 oldFigure = $(this).text();
                $(this).html("<input class=\"variableCostInput\" id=\""+$(this).attr('id')+"\" type=\"text\" value =\""+$(this).text()+"\"/>");
                $(".variableCostInput").focus();
            }
            else{
              $('.variableCostInput').blur();
              open = false;
              oldFigure = "";
            }

        });

        $(".variableCostInput").livequery(function(){
            $(this).keypress(function(e){
               if(e.which == "13"){
                   $(this).blur();
               }
               else if(e.which == 0){
                   //alert("youhuo");
                   //location.reload(true);
                   $(this).val(oldFigure);
                   $(this).blur();
               }
               else{
                 //  alert(e.which);
               }
            });
            $(this).blur(function(){
                open = false;
                var href = $(".urlUpdateVariableCost").attr("href");
                var id = $("#projectId").val();
                var type = $(this).attr('id');
                var val = $(this).val();
                val = validNumber(val);
               
                if(oldFigure != val ){
                     $(this).parent().text(val).append(imgLoad);
                     $.ajax({
                            url: href,
                            data: "ajaxCall=true&id="+id+"&idVariableCost="+type+"&figure="+val,
                            cache: false,
                            async: true,
                            success: function(html){
                                showAlert = false ;
                                //location.reload(true);
                                if(html != ""){
                                    $(".body").html(html);
                                }
                            }
                     });
                }
                else{
                     $(this).parent().html(oldFigure + imgPen);
                }
                oldFigure = "";
            })
        });



/********************************************************
 * EXPENDITURE CLICKABLE -
 *  - Project Detailed view
 ********************************************************/


        $(".expenditureFigures").live("dblclick", function(){
            if(!open){
                open = true;
                 oldFigure = $(this).text();
                $(this).html("<input class=\"expenditureInput\" id=\""+$(this).attr('id')+"\" type=\"text\" value =\""+$(this).text()+"\"/>");
                $(".expenditureInput").focus();
            }
            else{
              $('.expenditureInput').blur();
              open = false;
              oldFigure = "";
            }

        });

        $(".expenditureInput").livequery(function(){
            $(this).keypress(function(e){
               if(e.which == "13"){
                   $(this).blur();
               }
               else if(e.which == 0){
                   //alert("youhuo");
                   //location.reload(true);
                   $(this).val(oldFigure);
                   $(this).blur();
               }
               else{
                 //  alert(e.which);
               }
            });
            $(this).blur(function(){
                open = false;
                var href = $(".urlUpdate").attr("href");
                var id = $("#projectId").val();
                var type = $(this).attr('id');
                var val = $(this).val();
                val = validNumber(val);
                if(oldFigure != val ){
                    $(this).parent().text(val).append(imgLoad);
                     $.ajax({
                            url: href,
                            data: "ajaxCall=true&id="+id+"&type="+type+"&figure="+val,
                            cache: false,
                            async: true,
                            success: function(html){
                                showAlert = false ;
                                //location.reload(true);
                                if(html != ""){
                                 $(".body").html(html);
                                }
                            }
                     });
                }
                else{
                    $(this).parent().html(oldFigure + imgPen);
                }
                oldFigure = "";
            })
        });



/********************************************************
 * HOURS CLICKABLE -
 *  - Project & Course Detailed view
 ********************************************************/


        whoaId = "";
        $(".hoursFigures").live("dblclick", function(){
            if(!open){
                open = true;
                oldFigure = $(this).text();
                whoaId = $(this).attr('title');
                $(this).html("<input class=\"hoursInput\" id=\""+$(this).attr('id')+"\" type=\"text\" value =\""+$(this).text()+"\"/>");
                $(".hoursInput").focus();
            }
            else{
              $('.hoursInput').blur();
              open = false;
              oldFigure = "";
              whoaId = "";
            }
        });
        
        $(".hoursInput").livequery(function(){
            $(this).keypress(function(e){
               if(e.which == "13"){
                   $(this).blur();
               }
               else if(e.which == 0){
                   $(this).val(oldFigure);
                   $(this).blur();
               }
               else{
                 //  alert(e.which);
               }
            });
            $(this).blur(function(){
                open = false;
                var href = $(".urlUpdateHours").attr("href");
                var type = $(this).attr('id');
                var val = $(this).val();
                
                var page;
                var id;
                if($("#projectId") && $("#projectId").val()){
                    page = "project";
                    id=$("#projectId").val()
                }
                else if($("#ajaxEmployeeId") && $("#ajaxEmployeeId").val()){
                    page = "emp";
                    id=$("#ajaxEmployeeId").val()
                }
                else if($("#ajaxCourseId") && $("#ajaxCourseId").val()){
                    page = "course";
                    id=$("#ajaxCourseId").val()
                }
                if(oldFigure != val ){
                    val = validNumber(val);
                    $(this).parent().text(val).append(imgLoad);
                     $.ajax({
                            url: href,
                            data: "page="+page+"&ajaxCall=true&id="+id+"&whoaId="+whoaId+"&type="+type+"&figure="+val,
                            cache: false,
                            async: true,
                            success: function(html){
                               showAlert = false ;
                               //location.reload(true);
                               if(html != ""){
                                   $(".body").html(html);
                               }
                            }
                     });
                }
                else{
                    $(this).parent().html(oldFigure + imgPen);
                }
                oldFigure = "";
                whoaId = "";
            })
        });
        
        $(".editableFiguresArrow").live("click", function(){ // The the figures that are written on the page
            var href = $(".urlArrow").attr("href");
            var type = $(this).attr('id');
            var id = $("#ajaxEmployeeId").val();    // gets the employeeInstance.Id from bottom of Show (or whoa.id from project detailed view)
            
            var val = 0   //for input
            $(this).append(imgLoad);
             $.ajax({
                    url: href,
                    data: "ajaxCall=true&id="+id+"&type="+type+"&figure="+val,
                    cache: false,
                    async: true,
                    success: function(html){
                        showAlert = false ;
                        //location.reload(true)*
                        if(html != ""){
                            $(".body").html(html);
                        }
                    }
             });
        });

        $(".editableFiguresArrowHours").live("click", function(){
            var href = $(".urlHoursArrow").attr("href");
            var type = $(this).attr('id');
            var whoaId = $(this).attr('title');
            var val = 0   //for input
            var page;
            var id;
            if($("#projectId") && $("#projectId").val()){
                page = "project";
                id=$("#projectId").val()
            }
            else if($("#ajaxEmployeeId") && $("#ajaxEmployeeId").val()){
                page = "emp";
                id=$("#ajaxEmployeeId").val()
            }
            else if($("#ajaxCourseId") && $("#ajaxCourseId").val()){
                page = "course";
                id=$("#ajaxCourseId").val()
            }
            $(this).append(imgLoad);
             $.ajax({
                    url: href,
                    data: "page="+page+"&id="+id+"&ajaxCall=true&whoaId="+whoaId+"&type="+type+"&figure="+val,
                    cache: false,
                    async: true,
                    success: function(html){
                        showAlert = false ;
                        //location.reload(true)
                        if(html != ""){
                            $(".body").html(html);
                        }
                    }
             });
        });





        budgetId = "";
        $(".balanceFigures").live("dblclick", function(){
            if(!open){
                open = true;
                oldFigure = $(this).text();
                budgetId = $(this).attr('title');
                $(this).html("<input class=\"balanceInput\" id=\""+$(this).attr('id')+"\" type=\"text\" value =\""+$(this).text()+"\"/>");
                $(".balanceInput").focus();
            }
            else{
              $('.balanceInput').blur();
              open = false;
              oldFigure = "";
              budgetId = "";
            }
        });

        $(".balanceInput").livequery(function(){
            $(this).keypress(function(e){
               if(e.which == "13"){
                   $(this).blur();
               }
               else if(e.which == 0){
                   $(this).val(oldFigure);
                   $(this).blur();
               }
               else{
                 //  alert(e.which);
               }
            });
            $(this).blur(function(){
                open = false;
                var href = $(".urlUpdateBalance").attr("href");
                var type = $(this).attr('id');
                var val = $(this).val();
                
                if(oldFigure != val ){
                    val = validNumber(val);
                    $(this).parent().text(val).append(imgLoad);
                     $.ajax({
                            url: href,
                            data: "ajaxCall=true&budgetId="+budgetId+"&type="+type+"&figure="+val,
                            cache: false,
                            async: true,
                            success: function(html){
                               showAlert = false ;
                               //location.reload(true);
                               if(html != ""){
                                   $(".body").html(html);
                               }
                            }
                     });
                }
                else{
                    $(this).parent().html(oldFigure + imgPen);
                }
                oldFigure = "";
                budgetId = "";
            })
        });
/**************************************************************
 * Common function
 *
 **************************************************************/
        function validNumber(a){
            a = a.replace(/\s*/g,"");

           var b = parseFloat(a);
           if(isNaN(b)){
              // alert("You have not entered a valid number. It will be replaced by the previous number");
              return oldFigure;
           }
           else if(b < 0){
              // alert("You have entered a negative number. It will replaced by the previous number.");
              return oldFigure;
           }
           else return b;
        }

        //for courselist application
        $('#selectCourseId').change(function(){
           $('#form1').submit();
        });

        //for courselist application
        $('#selectScheduleId').change(function(){
           $('#form2').submit();
        });
        
 

    }
);



